let _state; // 放在外面防止每次调用被初始化掉

function render() {} // 渲染函数
function useState(initialValue) {
  _state = _state || initialValue;

  function setState(newState) {
    _state = newState;
    render();
  }
  // 返回数据为数据和更新函数
  return [_state, setState];
}

/**
 * 上述useState 没有考虑到，多次调用useState 的情况
 */

const Dispatcher = (function () {
  let firstInProgressHook = null,
    workInProgressHook = null,
    isMount = false;
  // update 阶段的hook
  function updateInProgressHook() {
    let curHook = workInProgressHook;
    workInProgressHook = workInProgressHook.next;
    return curHook;
  }
  // mount 阶段的hook
  function mountWorkInProgressHook() {
    let hook = {
      state: "",
      dispatch: null,
      next: null,
    };
    if (workInProgressHook) {
      workInProgressHook = firstInProgressHook = hook;
    } else {
      workInProgressHook = workInProgressHook.next = hook;
    }

    return workInProgressHook;
  }
  // 返回的useState 函数
  function useState(initialValue) {
    let hook;
    if (isMount) {
      hook = mountWorkInProgressHook();
      hook.state = initialValue;
    } else {
      hook = updateInProgressHook();
    }
  }
  // 返回的useEffect 函数
  function useEffect(initialValue, deps) {}
  return {
    useState,
    useEffect,
  };
})();
